package com.facebookproject.graph.getlist;

import static com.facebookproject.Const.APP_DATA;
import static com.facebookproject.Const.APP_FB_ID;
import static com.facebookproject.Const.APP_PATH;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Environment;

import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;
import com.facebookproject.graph.Holder;
import com.facebookproject.graph.element.Elements;
import com.facebookproject.loginfb.LoginFacebook;
@SuppressWarnings("deprecation")
public class GetFriendsList {
	String fPath = Environment.getExternalStorageDirectory().getAbsolutePath() + APP_PATH + APP_DATA;
	Holder fbHolder = new Holder();
	private Facebook facebook = new Facebook(APP_FB_ID);
	public List<Elements> getmyfriendslist(Holder fbHolder, boolean b){
		List<Elements> list = new ArrayList<Elements>();
		facebook = new LoginFacebook().login(fbHolder.context,facebook);
		try {
			JSONArray ja = null;
			Bundle params = new Bundle();
			params.putString("fields","work,education,location,mutualfriends,id,name");
			params.putString("access_token", facebook.getAccessToken());
//			File filereader = new File(fPath+"/Getmyfriendlist.zuk");
//			if(filereader.exists()){
//				JSONParser parser = new JSONParser();
//				Object obj = parser.parse(new FileReader(filereader));
//				ja = (JSONArray) obj;
//			}else{
				ja = Util.parseJson(facebook.request("me/friends",params,"GET")).getJSONArray("data");
//				savefile(ja);
//			}


			for (int i = 0; i < ja.length(); i++) {
				if(b)break;
				String id=null,name=null,ele1="Work : ",ele2="Lives in ",ele3="Studied ",ele4=null;
				JSONObject tmp = (JSONObject) ja.get(i);
				id = tmp.getString("id");
				name = tmp.getString("name");

				//get Work
				if(tmp.has("work")){
					JSONObject work = (JSONObject)tmp.getJSONArray("work").get(0);
					if(work.has("position"))ele1 = ele1 + work.getJSONObject("position").getString("name");
					if(work.has("employer"))ele1 = ele1 + " at " + work.getJSONObject("employer").getString("name");
					if(work.has("location"))ele1 = ele1 + ", " + work.getJSONObject("location").getString("name");
				}
				else{
					ele1=null;
				}

				//get Location
				if(tmp.has("location")){
					ele2 = ele2 + tmp.getJSONObject("location").getString("name");
				}
				else{
					ele2=null;
				}

				//Get Education
				if(tmp.has("education")){
					if(!tmp.has("work")){
						ele3 = "Study : ";
					}
					int x = tmp.getJSONArray("education").length();
					JSONObject edu  = (JSONObject)tmp.getJSONArray("education").get(x-1);
					if(edu.has("school"))ele3 = ele3 + edu.getJSONObject("school").getString("name");
				}
				else{
					ele3=null;
				}

				//Get Mutualfriends
				if(tmp.has("mutualfriends")){
					int x = tmp.getJSONObject("mutualfriends").getJSONArray("data").length();
					ele4 = x + " mutual friends";
					if(x>0){
						JSONObject fr1 = (JSONObject)tmp.getJSONObject("mutualfriends").getJSONArray("data").get(0);
						ele4 = ele4 + " including " + fr1.getString("name");
						if(x>1){
							JSONObject fr2 = (JSONObject)tmp.getJSONObject("mutualfriends").getJSONArray("data").get(1);
							ele4 = ele4 + " and " + fr2.getString("name") + "...";
						}
					}
				} 
				else{
					ele4=null;
				}
				//
				for(int y = 0;y < 3; y++)
					if(ele1==null){
						ele1=ele2;ele2=ele3;ele3=ele4;ele4=null;
					}
				for(int x = 0;x<2;x++)
					if(ele2==null){
						ele2=ele3;ele3=ele4;ele4=null;
					}
				if(ele3==null){
					ele3=ele4;ele4=null;
				}
				Elements ele = new Elements(id, name, ele1, ele2, ele3, ele4);
				list.add(ele);
			}	
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (FacebookError e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;

	}
	@SuppressWarnings("unused")
	private void savefile(JSONArray ja) {
		try {
			File dir = new File(fPath);
			if (!dir.exists()){
				dir.mkdirs();
			}
			FileWriter file = new FileWriter(fPath+"/Getmyfriendlist.zuk");
			file.write(ja.toString());
			file.flush();
			file.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
